﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class webadmin_pages_TaoTrang_list : System.Web.UI.Page
{
    public Common comm = new Common();
    public DataTable dtt;
    string id = ""; string act = ""; string sTemp = "";
    public string messageError = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this.Title = "Danh sách trang đã tạo";
            messageError = "";
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"])) id = comm.TextFormat(Request.QueryString["id"]);
                if (!string.IsNullOrEmpty(Request.QueryString["act"])) act = comm.TextFormat(Request.QueryString["act"]);

                switch (act)
                {
                    case "del":
                        if (!string.IsNullOrEmpty(id))
                        {
                            sTemp = "delete CreatePages_page_block where id_page='" + id + "'";
                            comm.execNoneQuery(sTemp);
                            sTemp = "delete CreatePages_page where id_page='" + id + "'";
                            comm.execNoneQuery(sTemp);
                            BindRepeater();
                        }
                        break;
                    default:
                        BindRepeater(); break;
                }
            }
        }
        catch (Exception ex)
        {
            BindRepeater();
            messageError = ex.Message.ToString();
        }
    }
    protected void LoadData(string sql)
    {
        DataTable dt = comm.getDataTable(sql);
        rpt_user_list.DataSource = dt;
        rpt_user_list.DataBind();

    }
    protected void rpt_user_list_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }

    protected void pagination1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        pagination1.CurrentIndex = currnetPageIndx;
        BindRepeater();
    }
    private void BindRepeater()
    {
        string ma = "%"; string ten = "%"; string sWhere = "";
        if (!string.IsNullOrEmpty(txtMa.Text)) ma = comm.TextFormat(txtMa.Text);
        if (!string.IsNullOrEmpty(txtTen.Text)) ten = comm.TextFormat(txtTen.Text);
        sWhere = "active=" + Byte.Parse(cboKichHoat.SelectedValue.ToString());
        sWhere += " and (id_page like '%" + ma + "%' and name_page like N'%" + ten + "%')";

        DataTable totalrow = comm.getDataTable("select id_page from CreatePages_page where " + sWhere);
        pagination1.ItemCount = totalrow.Rows.Count;
        //if (pagination1.CurrentIndex > pagination1.ItemCount) pagination1.CurrentIndex = 1;
        SqlParameter[] pa = new SqlParameter[5];

        pa[0] = new SqlParameter("@Tables", "CreatePages_page");
        pa[1] = new SqlParameter("@PK", "id_page");
        pa[2] = new SqlParameter("@PageNumber", pagination1.CurrentIndex);
        pa[3] = new SqlParameter("@PageSize", pagination1.PageSize);
        pa[4] = new SqlParameter("@Filter", sWhere);
        DataTable dt = comm.getDataTable(CommandType.StoredProcedure, "dbo.Paging_SubQuery", pa);

        rpt_user_list.DataSource = dt;
        rpt_user_list.DataBind();



    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindRepeater();
    }
    protected void btnSearch_Command(object sender, CommandEventArgs e)
    {
        BindRepeater();
    }
    protected void txtReset_Command(object sender, CommandEventArgs e)
    {
        txtTen.Text = "";
        txtMa.Text = "";
        BindRepeater();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Session.Remove("aRight"); Session.Remove("aCenter");
        Session.Remove("aCenterDouble");
        Response.Redirect("TaoTrang_frm.aspx?act=add");
    }
    protected void btnAdd_Command(object sender, CommandEventArgs e)
    {
        Session.Remove("aRight"); Session.Remove("aCenter");
        Session.Remove("aCenterDouble");
        Response.Redirect("TaoTrang_frm.aspx?act=add");
    }
}
